<?php

namespace App\Models;

use App\Models\CommonModel as Model;

/**
 * Class AdminModel
 * @package App\Models
 * @version May 12, 2018, 1:59 pm CST
 *
 */
class AdminModel extends Model
{

    public $table = 'admin';
    

    public $fillable = [
        'username',
        'mobile',
        'password',
        'is_active',
        'created_at'
    ];

    public $searchable = [
        'username',
        'mobile',
        'password',
        'is_active',
        'created_at'
    ];

    public $changeable = [
        'username',
        'mobile',
        'password',
        'is_active',
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        
    ];

    /**
     * @param $searchOptions
     * @return mixed
     */
    public function getQueryBySearchOptions($searchOptions)
    {
        $qb = self::newQuery();

        if (empty($searchOptions)) {
            return $qb;
        }

        !empty($searchOptions['keywords']) && $qb = $qb
            ->where('username', 'like', '%' . $searchOptions['keywords']. '%')
            ->orWhere('mobile', 'like', '%' . $searchOptions['keywords']. '%');
        unset($searchOptions['keywords']);

        foreach ($searchOptions as $key => $value) {
            if (is_array($value)) {
                $qb->whereIn($key, $value);
            } elseif (is_string($value) || is_numeric($value)) {
                $qb->where($key, $value);
            }
        }

        return $qb;
    }
}
